package com.idc.quotation.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "jwt")
@Data
public class WebConfig implements WebMvcConfigurer {

	private String[] skipUrls;

	@Autowired
	private JwtInterceptor jwtInterceptor;

    @Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(jwtInterceptor)
				.addPathPatterns("/**") // 拦截请求
				// .excludePathPatterns("/login/**", "/config/**"); // 排除登录请求
				.excludePathPatterns(skipUrls); // 排除请求
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许所有路径的跨域请求
				.allowedOrigins("*") // 允许的跨域请求
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
                .allowedHeaders("*"); // 允许的请求头
    }
}
